import csv

C_on = []
C_off = []
C_dc = []


def C_append(num, temp):
    global C_on, C_off, C_dc
    if num == 0:
        C_on.append(temp)
    elif num == 1:
        C_off.append(temp)
    elif num == 2:
        C_dc.append(temp)


def C_clear():
    global C_on, C_off, C_dc
    temp1, temp2, temp3 = C_on, C_off, C_dc
    C_on = [item for item in temp1 if item != '']
    C_off = [item for item in temp2 if item != '']
    C_dc = [item for item in temp3 if item != '']


# 读取CSV 文件
def read_MassCube():
    # 打开 CSV 文件
    with open('Mass_cube.csv', newline='', encoding='utf-8') as csvfile:
        csv_reader = csv.reader(csvfile)
        next(csv_reader)
        # 逐行读取 CSV 数据
        for row in csv_reader:
            for i in range(len(row)):
                C_append(i, row[i])
        C_clear()


# 吸收运算
def absorb(list1):
    list_absorb = []
    list1 = [x for x in list1 if x]
    if not list1:
        list_absorb = list1
    else:
        list1 = list(set(list1))
        num = len(list1)
        n = len(list1[0])
        list1_sign = [0] * num
        for i in range(num):
            for j in range(i+1, num):
                temp = []
                for k in range(n):
                    if list1[i][k] == list1[j][k]:
                        temp.append(0)
                    elif list1[i][k] == 'X':
                        temp.append(1)
                    elif list1[j][k] == 'X':
                        temp.append(2)
                    else:
                        temp.append(-1)
                if list1_sign[i] or list1_sign[j]:
                    continue
                elif (temp.count(1) != 0 and temp.count(2) != 0) or temp.count(-1) != 0:
                    continue
                elif temp.count(2) != 0:
                    list1_sign[i] = 1
                else:
                    list1_sign[j] = 1
        for i in range(num):
            if not list1_sign[i]:
                list_absorb.append(list1[i])
    return list_absorb


# 取并集
def union(list1, list2):
    list_result = []
    if not list1:
        list_result = list2
    elif not list2:
        list_result = list1
    else:
        list_result = list1 + list2
        list_result = absorb(list_result)
    return list_result


# 取交集
def intersection(list1, list2):
    list_result = []
    list1 = [x for x in list1 if x]
    list2 = [x for x in list2 if x]
    if not list1:
        list_result = list1
    elif not list2:
        list_result = list2
    else:
        for i in list1:
            for j in list2:
                temp = ''
                for k in range(len(i)):
                    if i[k] == '1' and j[k] == '0' or i[k] == '0' and j[k] == '1':
                        temp = ''
                        break
                    elif i[k] != 'X':
                        temp += i[k]
                    else:
                        temp += j[k]
                if temp:
                    list_result.append(temp)
        list_result = absorb(list_result)
    return list_result


# 普通星积
def convolution(list1, list2):
    convolution_result = ''
    temp = 0
    for i in range(len(list1)):
        if list1[i] == list2[i]:
            convolution_result += list1[i]
        elif list1[i] == 'X':
            convolution_result += list2[i]
        elif list2[i] == 'X':
            convolution_result += list1[i]
        else:
            temp += 1
            convolution_result += 'X'
    if temp != 1:
        convolution_result = ''
    return convolution_result


# C*C星积
def C_C_convolution(list1):
    C_C_convolution = []
    if not list1:
        print("星积的对象为空集，请重新设置")
    else:
        num = len(list1)
        for i in range(num):
            for j in range(i+1, num):
                temp = convolution(list1[i], list1[j])
                if temp:
                    C_C_convolution.append(temp)
    return C_C_convolution


# 广义星积法求质立方体
def generalized_convolution():
    global C_on, C_dc
    temp = union(C_on, C_dc)
    n = len(temp[0])
    for i in range(n):
        B0 = []
        B1 = []
        for j in temp:
            if j[i] == '0':
                B0.append(j[0:i] + 'X' + j[i+1:])
            elif j[i] == '1':
                B1.append(j[0:i] + 'X' + j[i+1:])
        temp = union(temp, intersection(B0, B1))
    z = []
    for i in temp:
        if intersection([i], C_on):
            z.append(i)
    return z


# 迭代星积法求质立方体
def iterative_convolution():
    global C_on, C_dc
    temp = union(C_on, C_dc)
    C_C_convolution(temp)
    a = union(temp, C_C_convolution(temp))
    while sorted(temp) != sorted(a):
        temp = a
        C_C_convolution(temp)
        a = union(temp, C_C_convolution(temp))
    z = []
    for i in a:
        if intersection([i], C_on):
            z.append(i)
    return z


# 无圈＃
def jin(list1, list2):
    jin_result = []
    if len(list1) == 1 and len(list2) == 1:
        temp = []
        for i in range(len(list1[0])):
            if list1[0][i] == list2[0][i] or list2[0][i] == 'X':
                temp.append('z')
            elif list1[0][i] == 'X':
                if list2[0][i] == '0':
                    temp.append('1')
                else:
                    temp.append('0')
            else:
                temp.append('y')
        if temp.count('y') >= 1:
            jin_result.append(list1[0])
        elif temp.count('z') == len(list1[0]):
            jin_result = []
        else:
            for i in range(len(temp)):
                if temp[i] == '0' or temp[i] == '1':
                    jin_result.append(list1[0][0:i] + temp[i] + list1[0][i+1:])
    elif len(list1) == 1:
        jin_result = jin(list1, [list2[0]])
        for i in range(len(list2) - 1):
            jin_result = jin(jin_result, [list2[i+1]])
    else:
        for i in list1:
            jin_result = union(jin_result, jin([i], list2))
    jin_result = absorb(jin_result)
    return jin_result


# 圈＃
def quan_jin(list1, list2):
    quan_jin_result = []
    if len(list1) == 1 and len(list2) == 1:
        temp = []
        for i in range(len(list1[0])):
            if list1[0][i] == list2[0][i] or list2[0][i] == 'X':
                temp.append('z')
            elif list1[0][i] == 'X':
                if list2[0][i] == '0':
                    temp.append('1')
                else:
                    temp.append('0')
            else:
                temp.append('y')
        if temp.count('y') >= 1:
            quan_jin_result.append(list1[0])
        elif temp.count('z') == len(list1[0]):
            quan_jin_result = []
        else:
            for i in range(len(temp)):
                if temp[i] == '0' or temp[i] == '1':
                    temp_1 = ''
                    for j in range(i):
                        if list1[0][j] == list2[0][j]:
                            temp_1 += list1[0][j]
                        elif list1[0][j] == 'X':
                            temp_1 += list2[0][j]
                        else:
                            temp_1 += list1[0][j]
                    quan_jin_result.append(temp_1 + temp[i] + list1[0][i+1:])
    elif len(list1) == 1:
        quan_jin_result = quan_jin(list1, [list2[0]])
        # print(quan_jin_result)
        for i in range(len(list2)-1):
            quan_jin_result = quan_jin(quan_jin_result, [list2[i+1]])
            # print(quan_jin_result)
    else:
        for i in list1:
            quan_jin_result = union(quan_jin_result, quan_jin([i], list2))
            # print(quan_jin_result)
    quan_jin_result = absorb(quan_jin_result)
    return quan_jin_result


# 锐积求质立方体
def accumulate():
    global C_on, C_dc, C_off
    C_off = quan_jin([len(C_on[0])*'X'], union(C_on, C_dc))
    temp = jin([len(C_on[0])*'X'], C_off)
    z = []
    for i in temp:
        if intersection([i], C_on):
            z.append(i)
    return z


read_MassCube()
print('C_on = ', C_on)
print('C_off = ', C_off)
print('C_dc = ', C_dc)
# C = union(C_on, C_dc)
# C = intersection(C_on, C_dc)
# C = absorb(C_on)
print('广义星积结果：', generalized_convolution())
print('迭代星积结果：', iterative_convolution())
print('锐积结果：', accumulate())
